package org.iteam.xlz.core.service.customerservice;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.iteam.core.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.customerservice.CustomerService;

public class CustomerServiceService extends BaseService<CustomerService>{
	static CustomerServiceService instance;
	private CustomerServiceService(){}
	
	public static CustomerServiceService getInstance(){
		if(null==instance){
			instance=new CustomerServiceService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from customservice where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<CustomerService> query(long page,long rows){
		try {
			return super.query("select * from customservice where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<CustomerService>();
		}
	}
	
	public boolean create(CustomerService model){
		SqlBuilder sql=SqlBuilder.createInsert("customservice");
		sql.addField("csid",UUID.randomUUID().toString().replace("-",""));
		sql.addField("cstid",model.getCstid());
		sql.addField("cusname",model.getCusname());
		sql.addField("cusphone",model.getCusphone());
		sql.addField("status",model.getStatus());
		sql.addField("creator",model.getCreator());
		sql.addField("createtime",DateUtil.currentTime());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(CustomerService model){
		SqlBuilder sql=SqlBuilder.createUpdate("customservice");
		sql.addField("cstid",model.getCstid());
		sql.addField("cusname",model.getCusname());
		sql.addField("cusphone",model.getCusphone());
		sql.addField("status",model.getStatus());
		sql.addWhere("csid",model.getCsid());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public CustomerService find(String mid){
		try {
			return super.show("select * from customservice where 1=1 and csid=?",mid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new CustomerService();
		}
	}
}
